package com.xwj.order.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xwj.order.domain.Item;
import com.xwj.order.feign.ItemFeignClient;
import com.xwj.order.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author xwj
 * @date 2019/12/17
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ItemFeignClient itemFeignClient;

    @Override
//    @HystrixCommand(fallbackMethod = "queryItemByIdFallbackMethod")
    public Item queryItemById(Long id) {
//        String itemUrl = "http://item-service/item/";
//        return restTemplate.getForObject(itemUrl + id, Item.class);
        return itemFeignClient.queryItemById(id);
    }

    /**
     * 请求失败执行的方法
     * fallbackMethod的方法参数个数类型要和原方法一致
     *
     * @param id
     * @return
     */
    public Item queryItemByIdFallbackMethod(Long id) {
        return new Item(id, "查询商品信息出错!", null, null, null);
    }
}
